package com.controller;

import com.entity.Relation;
import com.google.gson.Gson;
import com.service.impl.UserServiceImpl;
import com.service.intf.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RelationServlet extends BaseServlet{
    private UserService userService=new UserServiceImpl();
    /**
     * 查询用户的关注的人
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void  queryFollower(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");

        Integer followerNum = userService.queryFollowerNum(username);

        req.getSession().setAttribute("followerNum", followerNum);

        List<Relation> followerName = userService.queryFollowerName(username);

        req.getSession().setAttribute("followerName", followerName);

        String  queryUserName = (String) req.getSession().getAttribute("queryUserName");

//        if(queryUserName!=null){
//            username=queryUserName;
//        }

        //为了仍然停留在当前查询用户或本人的页面
//        resp.sendRedirect(req.getContextPath()+"/blogServlet?action=queryBlog&queryUserName="+queryUserName+"&username="+username);
//        req.getRequestDispatcher("/pages/blog/blog_list.jsp").forward(req, resp);
        req.getRequestDispatcher("/pages/follow/follower.jsp").forward(req, resp);
//        resp.sendRedirect(req.getContextPath()+"relationServlet?action=queryFollowed&username="+username);
    }




    /**
     * 查询用户的粉丝
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void  queryFollowed(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");

        Integer followedNum = userService.queryFollowedNum(username);

        req.getSession().setAttribute("followedNum", followedNum);

        List<Relation> followedName = userService.queryFollowedName(username);

        req.getSession().setAttribute("followedName", followedName);

        req.getRequestDispatcher("/pages/follow/followed.jsp").forward(req, resp);
//        resp.sendRedirect(req.getContextPath()+"/blogServlet?action=queryBlog&username="+username);
//        req.getRequestDispatcher("/pages/blog/blog_list.jsp").forward(req, resp);
    }

    protected void  addFollower(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String followerName = req.getParameter("followerName");
        String followedName = req.getParameter("followedName");
        userService.addFollower(followerName,followedName);
//        req.getRequestDispatcher("/pages/blog/blog_update.jsp").forward(req, resp);
        resp.sendRedirect(req.getContextPath()+"/relationServlet?action=isFollower&followerName="+followerName+"&followedName="+followedName);
//        resp.sendRedirect(req.getContextPath()+"/blogServlet?action=queryBlog&username="+followedName);

    }


    protected void  deleteFollower(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String followerName = req.getParameter("followerName");
        String followedName = req.getParameter("followedName");
        userService.deleteFollower(followerName,followedName);
//        req.getRequestDispatcher("/pages/blog/blog_update.jsp").forward(req, resp);
        resp.sendRedirect(req.getContextPath()+"/relationServlet?action=isFollower&followerName="+followerName+"&followedName="+followedName);
//        resp.sendRedirect(req.getContextPath()+"/blogServlet?action=queryBlog&username="+followedName);
    }

    protected void  isFollower(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String followerName = req.getParameter("followerName");
        String followedName = req.getParameter("followedName");

        boolean follower = userService.isFollower(followerName, followedName);
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("follower",follower);

        Gson gson=new Gson();
        String json = gson.toJson(resultMap);

        resp.getWriter().write(json);



    }
}
